//
//  BDFansTableViewCell.m
//  BadBoy
//
//  Created by wen on 2017/5/19.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDFansTableViewCell.h"

@interface BDFansTableViewCell ()

@property (nonatomic, strong) UIView *topLine;
@property (nonatomic, strong) UIView *bottomLine;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, copy) void(^clickBlock)(NSString *buttonTitle);

@end

@implementation BDFansTableViewCell

- (void)configeWithModel:(id)model fansType:(BDFansStartType)fansType clickBlock:(void(^)(NSString *buttonTitle))clickBlock{
    BOOL isFollow = NO;
    if (fansType ==BDFansStartTypeGuanzhu) {
        model = [model objectForKey:@"follow"];
    }else {
        isFollow = [model[@"is_followed"] boolValue];
        model = [model objectForKey:@"follower"];
    }
    if (model == nil || [model isEqual:[NSNull null]]) {
        self.imageView.image = [UIImage imageNamed:@"avatar"];
        self.textLabel.text = ASLocalizedString(@"该用户不存在");
        self.button.hidden = YES;
    }else {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:model[@"cover"]] placeholderImage:[UIImage imageNamed:@"avatar"]];
        self.textLabel.text = model[@"nickname"];
        self.button.hidden = NO;
        if (fansType == BDFansStartTypeGuanzhu) {
            [self.button setTitle:ASLocalizedString(@"取消关注")forState:UIControlStateNormal];
            self.button.userInteractionEnabled = YES;
        }else {
            if (isFollow == YES) {
                [self.button setTitle:ASLocalizedString(@"已关注")forState:UIControlStateNormal];
                self.button.userInteractionEnabled = NO;
            }else {
                [self.button setTitle:ASLocalizedString(@"关注")forState:UIControlStateNormal];
                self.button.userInteractionEnabled = YES;
            }
        }
        self.clickBlock = clickBlock;
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.imageView.size = CGSizeMake(self.height - 20, self.height - 20);
    self.imageView.left = 10;
    self.imageView.centerY = self.height / 2.0;
    self.imageView.layer.masksToBounds = YES;
    self.imageView.layer.cornerRadius = self.imageView.height / 2.0;
    
    self.textLabel.font = [UIFont systemFontOfSize:14];
    self.textLabel.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
    [self.textLabel sizeToFit];
    self.textLabel.left = self.imageView.right + 10;
    self.textLabel.centerY = self.imageView.centerY;
    
    NSString *fansTitle = [self.button titleForState:UIControlStateNormal];
    if ([fansTitle isEqualToString:ASLocalizedString(@"已关注")]) {
        [self.button setTitleColor:[UIColor colorWithHexString:@"#767676"] forState:UIControlStateNormal];
        self.button.backgroundColor = [UIColor whiteColor];
        self.button.layer.borderColor = kBDColorGray.CGColor;
        self.button.layer.borderWidth = 0.5;
    }else {
        [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.button.backgroundColor = KBDColorBlue;
        self.button.layer.borderColor = kBDColorGray.CGColor;
        self.button.layer.borderWidth = 0;
    }
    self.button.titleLabel.font = [UIFont systemFontOfSize:14];
    
    self.button.size = CGSizeMake(80, 30);
    self.button.right = self.width - 10;
    self.button.centerY = self.imageView.centerY;
    self.button.layer.masksToBounds = YES;
    self.button.layer.cornerRadius = 4;
    
    self.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.topLine.width = self.width;
    self.topLine.height = 1;
    self.topLine.top = 0;
    self.topLine.left = 0;
    
    self.bottomLine.width = self.width;
    self.bottomLine.height = 1;
    self.bottomLine.bottom = self.height;
    self.bottomLine.left = 0;
    
    if (self.isFirst == YES) {
        self.topLine.hidden = NO;
    }else {
        self.topLine.hidden = YES;
    }
}

- (void)buttonAction{
    if (self.clickBlock) {
        self.clickBlock([self.button titleForState:UIControlStateNormal]);
    }
}

- (UIView *)topLine{
    if (!_topLine) {
        self.topLine = [[UIView alloc] init];
        _topLine.backgroundColor = kBDColorGray;
        [self.contentView addSubview:_topLine];
    }
    return _topLine;
}

- (UIView *)bottomLine{
    if (!_bottomLine) {
        self.bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = kBDColorGray;
        [self.contentView addSubview:_bottomLine];
    }
    return _bottomLine;
}

- (UIButton *)button{
    if (!_button) {
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_button];
    }
    return _button;
}

@end
